/*
 *  HASFLAVR.cpp
 *  TubeTruck
 *
 *  Created by Aeturnum on 6/6/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "HASFLAVR.h"

using namespace std;
using namespace TTP;

/*
 byte mFlags;

 enum {HASFLAVR_yes = 0x01, HASFLAVR_no = 0x02, HASFLAVR_votesNeeded = 0x04};
 */
 
HASFLAVR::HASFLAVR() : Packet(Packet_HASFLAVR)
{}
HASFLAVR::HASFLAVR(id_hash idHash) : Packet(idHash, Packet_HASFLAVR)
{}
HASFLAVR::HASFLAVR(id_hash idHash, vector<byte>& sig , byte flags) :
Packet(idHash, Packet_HASFLAVR), Signable(sig), mFlags(flags)
{}
HASFLAVR::~HASFLAVR() {}
 
 // accessors
byte HASFLAVR::getFlags() const {
   return mFlags;
}

bool HASFLAVR::getFlag(byte flag) const {
   return mFlags & flag;
}

vector<byte> HASFLAVR::getBlowfishIv() {
   return mBlowfishIv;
}

// modifiers
void HASFLAVR::setFlags(byte flags) {
   mFlags = flags;
}

void HASFLAVR::setFlag(byte flag) {
   if(getFlag(flag))
      mFlags = mFlags & ~flag;
   else
      mFlags = mFlags | flag;
}
 
void HASFLAVR::setBlowfishIv(std::vector<byte>& blowfishIv) {
   mBlowfishIv = blowfishIv;
}

 //idHash
void HASFLAVR::setSourceIdHash(id_hash idHash) {
   mIdHash = idHash;
}

uint32_t HASFLAVR::getSourceIdHash() const {
   return mIdHash;
}
 
 // inherited
void HASFLAVR::fromVec(vector<byte>::const_iterator &vit, vector<byte>::const_iterator &end) {
   mBlowfishIv.clear();
   mEncryptedSection.clear();
   Packet::fromVec(vit, end);
   mSig.fromVec(vit, end);
   mBlowfishIv.insert(mBlowfishIv.end(), vit, vit + 8);
   vit = vit + 8;
   
   mEncryptedSection.insert(mEncryptedSection.end(), vit, end);
   
   mEncrypted = true;
}

void HASFLAVR::toVec(vector<byte> &vec) const {
   if(!mEncrypted) {
      throw msg_exception("ERROR:toVec: DOWANT not encrypted!");
   } else {
      if(!mSigned) {
         throw msg_exception("ERROR:DOWANT not signed!");
      } else {
         if(mBlowfishIv.empty()) {
            throw msg_exception("ERROR: Blowfish IV not set!");
         } else {
            Packet::toVec(vec);
            mSig.toVec(vec);
            vec.insert(vec.end(), mBlowfishIv.begin(), mBlowfishIv.end());
            vec.insert(vec.end(), mEncryptedSection.begin(), mEncryptedSection.end());
         }
      }
   }
}
 
vector<byte> HASFLAVR::getEncryptedSection() {
   if(!mEncrypted) {
      fillEncryptedSection();
   }
   return mEncryptedSection;
   
}

void HASFLAVR::fillEncryptedSection() {
   if(mEncryptedSection.size() != 1) {
      mEncryptedSection.clear();
      mEncryptedSection.push_back(mFlags);
   }
}

void HASFLAVR::setEncryptedData(vector<byte>& encData) {
   mEncryptedSection = encData;
   mEncrypted = true;
}

void HASFLAVR::setPlainText(std::vector<byte>& unencData) {
   mFlags = unencData[0];
}

vector<byte> HASFLAVR::getUnsignedData() const {
   vector<byte> toSign;
   vector<byte> blankSig = mSig.getBlankSigniture();
   
   Packet::toVec(toSign);
   toSign.insert(toSign.end(), blankSig.begin(), blankSig.end());
   toSign.insert(toSign.end(), mBlowfishIv.begin(), mBlowfishIv.end());
   toSign.insert(toSign.end(), mEncryptedSection.begin(), mEncryptedSection.end());
   return toSign;
}